#!/usr/bin/env python
# -*- coding: utf-8 -*
import clutter
from clutter import Stage, Group, Label, Color
from gstmanager.event import EventListener
import gobject

class InfoBar(Group, EventListener):
    def __init__(self):
        EventListener.__init__(self)
        Group.__init__(self)
        self.stage = Stage()

        info = self.info = Label()
        info.show()
        info.set_line_wrap(True)
        info.set_text("Informations")

        info.set_color(clutter.color_parse("Black"))
        info.set_font_name("Arial 15")

        self.height = 23
        info.set_width(self.stage.get_width())

        self.add(info)
        self.show()
        self.registerEvent("info")

    def evt_info(self, event):
        self.set_info(event.content)

    def set_info(self, text):
        text = text.replace("\n", " ")
        self.info.set_text(text)
        ih = self.info.get_height()
        h = self.height 
        if ih > h:
            ratio = h/float(ih)
            nb_lines = ih / h
            nb_letters = len(self.info.get_text())
            nb_letters_per_line = nb_letters/nb_lines
            words = text.split(" ")

            line = ""
            timeout = 0
            for word in words:
                if len(line) + len(word) - 5 < nb_letters_per_line:
                    line = "%s %s" %(line, word)
                else:
                    gobject.timeout_add(timeout, self.info.set_text, line)
                    timeout += 1000
                    line = ""
            if line != "":
                gobject.timeout_add(timeout, self.info.set_text, line)
